﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using SiteManager;
using System.Data.SqlClient;

public partial class dicountDoctorDentist_doctorAppointmentDetail : System.Web.UI.Page
{
    Manager mgr = new Manager(true, "ConnectionString");
    Manager objManager = new Manager(true, "ConnectionString");
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //int doctorId = Convert.ToInt32(Session["eventId"]);

            int eventID = 0;
            if (!string.IsNullOrEmpty(Request.QueryString["Id"]))
            {
                eventID = Convert.ToInt32(Request.QueryString["Id"]);
            }
            else
            {
                eventID = Convert.ToInt32(Session["eventId"]);
            }

            GetDoctorsApointmentDetialsResult doc = CalenderRepository.GetDoctorsApointmentDetials(eventID);
           if (doc != null)
           {
               ////String fName = doc.FName;
               ////String mName = doc.MName;
               ////String lName = doc.LName;

               ////string fullName = fName + mName + lName;

               lblWith.Text = doc.docname;
               lblWhen.Text = doc.eventstart.ToString();
               lblWhere.Text = doc.Address1.ToString();
               LabelPhone.Text = doc.phone;
               int docId =Convert.ToInt32( doc.doctorID);
               BindBOx(docId);
           }

        }


    }

    private void BindBOx(int id)
    {
        
       // int doctorId = Convert.ToInt32(Session["eventId"]);
        int eventId = 0;
        if (!string.IsNullOrEmpty(Request.QueryString["Id"]))
        {
            eventId = Convert.ToInt32(Request.QueryString["Id"]);
        }
        else
        {
            eventId = Convert.ToInt32(Session["eventId"]);
        }

        GetDoctorsApointmentDetialsResult doc = CalenderRepository.GetDoctorsApointmentDetials(eventId);
        int did =Convert.ToInt32( doc.doctorID);
       int  dr_id = did;
        SqlParameter[] sqlParam = new SqlParameter[1];
        sqlParam[0] = new SqlParameter("@dr_id", SqlDbType.Int);
        sqlParam[0].Value = id;

        DataSet ds_drdet = CustomRepository.CustumDataSetFunction("get_doctordetails", sqlParam, "tbl_a");
        //DataSet ds_drdet = mgr.ExecuteQuery("get_doctordetails", sqlParam, "tbl_a");
        //DataSet ds_drdet= new DataSet();
        //= mgr.ExecuteQuery("get_doctordetails", sqlParam, "tbl_a");

        
        
        
      
        string str_path = String.Empty;
        if (System.IO.File.Exists(Request.PhysicalApplicationPath + "/images/Dr_images/" + ds_drdet.Tables[0].Rows[0]["photo"].ToString()))
        {
            if (ds_drdet.Tables[0].Rows[0]["photo"].ToString().Trim().Length > 0)
            {
                str_path = getThumbnaleImageWithURLFormat(ds_drdet.Tables[0].Rows[0]["photo"].ToString(), 130, 100, false, "", Session["strHTTP"].ToString());
            }
            else
            {
                str_path = getThumbnaleImageWithURLFormat("silhouette-male.gif", 80, 100, false, "", Session["strHTTP"].ToString());
                //string str_path = mgr.getThumbnaleImage("../images/Dr_images/" + ds_drdet.Tables[0].Rows[0]["photo"].ToString(), 130, 100, false, "");                   
            }
        }
        else
        {
            str_path = getThumbnaleImageWithURLFormat("silhouette-male.gif", 80, 100, false, "", Session["strHTTP"].ToString());
            //string str_path = mgr.getThumbnaleImage("../images/Dr_images/" + ds_drdet.Tables[0].Rows[0]["photo"].ToString(), 130, 100, false, "");                   
        }

        gender.Text = ds_drdet.Tables[0].Rows[0]["gender"].ToString();
        imgTag.Text = str_path;

    }
    public string getPhoto(object photo, object sex)
    {
        if (photo != null)
        {

            string p = photo.ToString();
            if (p.Length > 0)
            {


                if (File.Exists(Request.PhysicalApplicationPath + "\\images\\Dr_images\\" + photo))
                {
                    //string str_path = mgr.getThumbnaleImage(Session["strHttp"].ToString() + "images/Dr_images/" + photo, 80, 100, false, "");

                    //Ian.........
                    string str_path = getThumbnaleImageWithURLFormat(photo.ToString(), 50, 60, false, "", Session["strHTTP"].ToString());
                    return str_path;
                }
                else
                {
                    if (sex.ToString().ToLower() == "m")
                        return "<img height=\"60\" width=\"50\" src='" + Session["strHttp"].ToString() + "images/Dr_images/silhouette-male.gif'>";
                    else
                        return "<img height=\"60\" width=\"50\" src='" + Session["strHttp"].ToString() + "images/Dr_images/silhouette--female.gif'>";
                }
            }
            else
            {
                if (sex.ToString().ToLower() == "m")
                    return "<img height=\"60\" width=\"50\" src='" + Session["strHttp"].ToString() + "images/Dr_images/silhouette-male.gif'>";
                else
                    return "<img height=\"60\" width=\"50\" src='" + Session["strHttp"].ToString() + "images/Dr_images/silhouette--female.gif'>";
            }
        }
        return null;


    }

    public string getThumbnaleImageWithURLFormat(string imgName, int maxWidth, int maxHeight, bool saveImage, string savePath, string baseURL)
    {
        string s;
        double imgHeight, imgWidth;
        //Response.Write(imgName) ;
        s = Server.MapPath("~/images/dr_images/" + imgName);
        //Response.Write("</br>"+s);
        System.Drawing.Image currentImage = System.Drawing.Image.FromFile(s);
        imgHeight = currentImage.Height;
        imgWidth = currentImage.Width;
        if (imgWidth > maxWidth | imgHeight > maxHeight)
        {
            double deltaWidth = imgWidth - maxWidth;
            double deltaHeight = imgHeight - maxHeight;
            double scaleFactor;
            if (deltaHeight > deltaWidth)
                scaleFactor = maxHeight / imgHeight;
            else
                scaleFactor = maxWidth / imgWidth;
            imgWidth *= scaleFactor;
            imgHeight *= scaleFactor;
        }

        if (saveImage)
        {
            if (savePath == "")
                throw new Exception("must pass a file name");
            currentImage.GetThumbnailImage(Convert.ToInt32(imgWidth), Convert.ToInt32(imgHeight), null, IntPtr.Zero).Save(Server.MapPath(savePath));
            currentImage.Dispose();
        }
        //return currentImage.GetThumbnailImage(Convert.ToInt32(imgWidth), Convert.ToInt32(imgHeight), null, IntPtr.Zero);


        return ("<img src=\"" + baseURL + "images/dr_images/" + imgName + "\"" + " height=\"" + imgHeight + "\" width=\"" + imgWidth + "\" border='0' alt=''/>");
    }

    protected void btnBackToSearch_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/patient/PatientCalendar.aspx?Id=" + Session["userid"].ToString());
    }
}
